<?php
class Pageley_Gateway_Database_SimpleColumnNameGenerator implements Pageley_Gateway_Database_ColumnNameGenerator {
    
    private $tableAlias;
    private $type;
    private $tableName;
    private $delimiter;
    private $columnNames;
    private $columnNameMapping;
    
    public function __construct($type, $tableName, $tableAlias = null, $delimiter = "") {
        $this->type = $type;
        $this->tableName = $tableName;
        $this->tableAlias = $tableAlias;
        $this->delimiter = $delimiter;
        $this->columnNameMapping = new Pageley_Util_ArrayMap();
    }
    
    public function getColumnNames($includeId = true) {
        $columnNames = $this->columnNames;
        if($columnNames === null) {
            $first = true;
            if($includeId) {
                $columnNames = $this->formatColumnName($this->getIdColumnName()) . ", ";
            }
            foreach($this->type->getAttributes() as $propertyName => $attribute) {
                if(!$attribute->isMultiple()) {
                    $columnName = $this->getColumnName($propertyName, $attribute);
                    // TODO: nested composites
                    if($attribute->isComposite()) {
                        foreach($attribute->getAttributes() as $subPropertyName => $subAttribute) {
                            $subColumnName = $this->getColumnName($propertyName . "." . $subPropertyName, $subAttribute);
                            if($first) {
                                $first = false;
                            } else {
                                $columnNames .= ", ";
                            }
                            $columnNames .= $this->formatColumnName($subColumnName);
                        }
                    } else {
                        if($first) {
                            $first = false;
                        } else {
                            $columnNames .= ", ";
                        }
                        $columnNames .= $this->formatColumnName($columnName);
                    }
                }
            }
            $this->columnNames = $columnNames;
        }
        return $columnNames;
    }
    
    public function getIdColumnName() {
        return $this->tableName . "_id";
    }
    
    public function getColumnName($propertyName, $attribute = null) {
        $columnName = $this->columnNameMapping->get($propertyName);
        if($columnName === null) {
            if($propertyName == "id") {
                $columnName = $this->getIdColumnName();
            } else {
                if($attribute === null) {
                    // TODO: nested composite
                    $attribute = $this->type->getAttribute($propertyName);
                }
                $columnName = Pageley_Util_String::addUnderscores($propertyName);
                if($attribute->isReference()) {
                    $columnName .= "_id";
                }
                $columnName = Pageley_Util_String::replace($columnName, ".", "_");
            }
            $this->columnNameMapping->put($propertyName, $columnName);
        }
        return $columnName;
    }
    
    protected function getDelimiter() {
        return $this->delimiter;
    }
    
    public function formatColumnName($columnName) {
        return ($this->tableAlias != null ? $this->tableAlias . "." : "") . $this->delimiter . $columnName . $this->delimiter;
    }
}